package com.ruoyi.web.sso.api;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.web.sso.common.util.AssertUtil;
import com.ruoyi.web.sso.common.util.BusinessException;
import com.ruoyi.web.sso.common.util.ErrorCode;
import com.ruoyi.web.sso.domain.Alliance;
import com.ruoyi.web.sso.domain.DTO.StatusResponse;
import com.ruoyi.web.sso.domain.PushAddress;
import com.ruoyi.web.sso.domain.PushDeviceInfo;
import com.ruoyi.web.sso.domain.vo.AllPushNews;
import com.ruoyi.web.sso.domain.vo.PushDevice;
import com.ruoyi.web.sso.domain.vo.PushNews;
import com.ruoyi.web.sso.service.IAllianceService;
import com.ruoyi.web.sso.service.IPushAddressService;
import com.ruoyi.web.sso.service.IPushDeviceInfoService;
import com.ruoyi.web.sso.service.IPushRecordService;
import com.ruoyi.web.sso.util.CopyUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 加盟信息Controller
 *
 * @author ruoyi
 * @date 2022-01-08
 */
@Controller
@RequestMapping("/api/sso/push")
@Api(tags = "消息推送接口")
public class ApiPushController extends BaseController {


    @Autowired
    private IPushRecordService pushRecordService;

    @Autowired
    private IPushAddressService addressService;

    @Autowired
    private IPushDeviceInfoService deviceInfoService;

    /**
     * 推送消息
     */
    @Log(title = "推送消息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    @ApiOperation(value = "推送消息", notes = "推送消息")
    public StatusResponse addPush(PushNews pushNews) {
        pushRecordService.addPush(pushNews);
        return StatusResponse.success("发送成功");
    }


    /**
     * 指定APP推送全体消息
     */
    @Log(title = "allPush", businessType = BusinessType.INSERT)
    @PostMapping("/allPush")
    @ResponseBody
    @ApiOperation(value = "指定APP推送全体消息", notes = "指定APP推送全体消息")
    public StatusResponse allPush(AllPushNews allPushNews) {
        pushRecordService.allPush( allPushNews);
        return StatusResponse.success("发送成功");
    }

    /**
     * 添加设备
     */
    @Log(title = "添加设备", businessType = BusinessType.INSERT)
    @PostMapping("/addDeviceInfo")
    @ResponseBody
    @ApiOperation(value = "添加设备", notes = "添加设备")
    public StatusResponse addDeviceInfo(PushDevice pushDevice) {
        ;PushDeviceInfo pushDeviceInfo = new PushDeviceInfo();
        CopyUtils.copyProperties(pushDevice,pushDeviceInfo);
        if (AssertUtil.isEmpty(deviceInfoService.selectPushDeviceInfoList(pushDeviceInfo))) {
            deviceInfoService.insertPushDeviceInfo(pushDeviceInfo);
        }else {
            throw new BusinessException(ErrorCode.UNDEFINED, "设备已存在");
        }
        return StatusResponse.success("添加成功");
    }

}
